package com.mao.controller;

import com.mao.common.ImageUD;
import com.mao.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;

/**
 * 上传下载文件管理(主要图片)
 */
@RestController
@RequestMapping("/common")
@Slf4j
public class CommonController {
   @Autowired
    private ImageUD imageUD;

    /**
     * 将后台上传的图片下载到本地
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file){
        String fileName = imageUD.upload(file);
        return Result.success(fileName);
    }

    /**
     * 读取本地图片再写到浏览器展示
     * @param name
     * @param response
     */
    @GetMapping("/download")
    public void download(String name, HttpServletResponse response){
        imageUD.download(name, response);
    }
}
